<?php

namespace app\model;

use app\utils\DateUtil;
use think\Model;

class CustomerLoanAccount extends Model
{

    protected $table = 'customer_loan_account';

    public function modifyDataById(int $loanAccountId, array $data)
    {
        return $this->where(['id' => $loanAccountId])->update($data);
    }

    public function loanAccountList(int $customerId)
    {
        $fields = [
            'id as loan_account_id',
            '`organization_name`',
            '`start_time`',
            '`end_time`',
            '`total_amount`',
            '`balance_amount`',
            '`repayment_style`',
            '`repayment_amount`',
            '`repayment_day`',
            '`business_lines`',
            '`guarantee_style`',
            '`is_settle`',
            '`settle_time`',
            '`loan_type`'
        ];
        return $this
            ->where([['customer_id', '=', $customerId], ['is_settle', '=', '1']])
            ->field($fields)
            ->select()
            ->toArray();
    }

    public function expireNotice()
    {
        $fields = [
            'id as loan_account_id',
            '`customer_id`',
            '`organization_name`',
            '`start_time`',
            '`end_time`',
            '`total_amount`',
            '`balance_amount`',
            '`repayment_style`',
            '`repayment_amount`',
            '`repayment_day`',
            '`business_lines`',
            '`guarantee_style`',
            '`is_settle`',
            '`settle_time`',
        ];
        $date = date('Y-m-d', time() + 24 * 3600 * 60);
        return $this
            ->where([['end_time', '>=', date('Y-m-d')], ['end_time', '<=', $date], ['is_settle', '=', '1']])
            ->field($fields)
            ->select()
            ->toArray();
    }

    public function repaymentNotice()
    {
        $fields = [
            'id as loan_account_id',
            '`organization_name`',
            '`start_time`',
            '`end_time`',
            '`total_amount`',
            '`balance_amount`',
            '`repayment_style`',
            '`repayment_amount`',
            '`repayment_day`',
            '`business_lines`',
            '`guarantee_style`',
            '`is_settle`',
            '`settle_time`',
            '`customer_id`',
            '`loan_type`'
        ];
        return $this
            ->where([['repayment_day', '=', DateUtil::twoDay()], ['is_settle', '=', '1']])
            ->field($fields)
            ->select()
            ->toArray();
    }
}